package com.vieewer.btdemo.view.microsoft.robot

import android.os.Bundle
import android.util.Log
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import kotlinx.android.synthetic.main.activity_luis_robot.*
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.utils.URIBuilder
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import com.alibaba.fastjson.serializer.AwtCodec.support
import android.support.v4.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T



class LuisRobotActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_luis_robot)
        test_luis_predict.setOnClickListener {
            predict()
        }
    }

    fun predict () {

        Thread {
            val httpclient = HttpClients.createDefault()
            try {

                    // The ID of a public sample LUIS app that recognizes intents for turning on and off lights
                    val AppId = "377c02c4-f1ef-42ec-89c2-55757fa9421e"

                    // Add your endpoint key
                    val Key = "71817deaf43a45b2a63eb7e23dbd3002"

                    // Add your endpoint, example is westus.api.cognitive.microsoft.com

                    val Endpoint = "westus.api.cognitive.microsoft.com"

                    val Utterance = "Turn off the lights"

                // Begin endpoint URL string building
                val endpointURLbuilder =
                    URIBuilder("https://$Endpoint/luis/prediction/v3.0/apps/$AppId/slots/production/predict?")


                    // query string params
                    endpointURLbuilder.setParameter("query", Utterance)
                    endpointURLbuilder.setParameter("subscription-key", Key)
                    endpointURLbuilder.setParameter("show-all-intents", "true")
                    endpointURLbuilder.setParameter("verbose", "true")

                    // create URL from string
                    val endpointURL = endpointURLbuilder.build()

                    // create HTTP object from URL
                    val request = HttpGet(endpointURL)

                    // access LUIS endpoint - analyze text
                    val response = httpclient.execute(request)

                    // get response
                    val entity = response.entity


                    if (entity != null) {
                        Log.i("yjj", "response >>>>. " + EntityUtils.toString(entity))
                    }

            } catch (e: Exception) {
                println(e.message)
            }

        }.start()

    }


}